//
//  SYPiCuoFanKuiView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/8/29.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYPiCuoFanKuiView.h"
#import "SYPiCuoTopView.h"
#import "SYNoLabel.h"
#import "SYPiCuoTableViewCell.h"
#import "SYXiuGaiDetailsView.h"

@interface SYPiCuoFanKuiView ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *curTableView;//表
@property (strong, nonatomic) SYPiCuoTopView *pcTopView;//批错头部
@property (strong, nonatomic) SYNoLabel *noLab;
@property (strong, nonatomic) NSMutableArray *dataListArr;
@property (strong, nonatomic) SYXiuGaiDetailsView *xiugaiView;

@end


@implementation SYPiCuoFanKuiView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    self.dataListArr = [NSMutableArray array];
    self.pcTopView = [[NSBundle mainBundle] loadNibNamed:@"SYPiCuoTopView" owner:nil options:nil].firstObject;
    [self addSubview:self.pcTopView];
    [self.pcTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(45);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.equalTo(self.pcTopView.mas_bottom);
    }];
    __weak typeof(self) weakSelf = self;
    [self.curTableView headerRefreshBlock:^{
        [self.dataListArr removeAllObjects];
        [weakSelf requestDataPage:1];
    }];
    
    [self.curTableView fooderRefreshBlock:^{
        NSInteger page = self.dataListArr.count/10 +1;
        [weakSelf requestDataPage:page];
    }];
    
    self.noLab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self];
    
    [self.noLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pcTopView.mas_bottom);
        make.left.right.bottom.equalTo(self);
    }];
}

- (void)requestDataPage:(NSInteger)page {
    
    NSDictionary *parmDic = @{@"pageIndex":SJStringWithFormat(page),
                              @"pageSize":@"10",
                              @"isHandle":@""
                              };
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI postDataFromURLScheme:@"paper/CorrectService.svc/GetFeedbackQuestionList" parameterDictionary:parmDic success:^(id responseObject) {
        [weakSelf.curTableView.mj_header endRefreshing];
        if (page >1) {
            if (weakSelf.dataListArr.count == 0) {
                [weakSelf.curTableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [weakSelf.curTableView.mj_footer endRefreshing];
            }
            
        } else {
            [weakSelf.curTableView.mj_footer endRefreshing];
        }
        NSArray *dataArr = responseObject[@"data"];
        
        if (dataArr.count <10) {
            [weakSelf.curTableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [weakSelf.dataListArr addObjectsFromArray:dataArr];
        [weakSelf.curTableView reloadData];
        
    } fail:^(NSError *error) {
        self.noLab.hidden = NO;
        [weakSelf.curTableView.mj_footer endRefreshing];
        [weakSelf.curTableView.mj_header endRefreshing];
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
    }];
    
}

#pragma mark - set
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataListArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYPiCuoTableViewCell *pcCell = [tableView dequeueReusableCellWithIdentifier:@"SYPiCuoTableViewCell" forIndexPath:indexPath];
    if (indexPath.row %2) {
        pcCell.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    } else {
        pcCell.backgroundColor = [UIColor whiteColor];
    }
    pcCell.selectionStyle = UITableViewCellSelectionStyleNone;
    pcCell.xuhaoLab.text = SJStringWithFormat(indexPath.row +1);
    pcCell.dict = self.dataListArr[indexPath.row];
    
    return pcCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.dataListArr[indexPath.row];
    _xiugaiView = [[SYXiuGaiDetailsView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 0, kScreen_WIDTH, kScreen_HEIGHT) ];
    //    _xiugaiView.backgroundColor = [UIColor redColor];
    [[UIApplication sharedApplication].keyWindow addSubview:_xiugaiView];
    __weak typeof(self) weakSelf = self;
    SYCorrectLogModel *logModel = [[SYCorrectLogModel alloc] init];
    logModel.correctId = [dict nilObjectForKey:@"answerIds"];
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.xiugaiView.frame = CGRectMake(0, 0, kScreen_WIDTH, kScreen_HEIGHT);
    } completion:^(BOOL finished) {
        [weakSelf.xiugaiView requestDataListAnswerIds:[dict nilObjectForKey:@"answerIds"]];
    }];
}

#pragma mark -

- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        [_curTableView registerCellWithNIbNames:@[@"SYPiCuoTableViewCell"]];
    }
    return _curTableView;
}
@end
